package com.homedo.odin.solution.controller.pc;

import com.homedo.core.common.base.controller.BaseController;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.model.request.pc.brand.BrandListRequestBean;
import com.homedo.odin.solution.model.response.pc.brand.BrandFirmDetailResponseBean;
import com.homedo.odin.solution.model.response.pc.brand.BrandOwnerDetailResponse;
import com.homedo.odin.solution.service.BrandOwnerServiceImpl;
import com.homedo.odin.solution.service.bizservice.mng.MngBrandBizService;
import com.homedo.odin.solution.service.bizservice.mng.MngBrandFirmsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: donglibing
 * @date: 2018/3/27 17:30
 * @Description:
 */
@Api(description = "品牌", tags = {"Brand"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/brands")
public class BrandController extends BaseController<MngBrandBizService> {

    @Autowired
    private BrandOwnerServiceImpl brandOwnerService;

    @Autowired
    private MngBrandFirmsService brandFirmsService;

    @ApiOperation(value = "PC_获取品牌列表", notes = "包含老接口：v1/pc/common/data/brand/owners",
            response = BrandOwnerDetailResponse.class, httpMethod = "GET")
    @GetMapping()
    public ResultData queryAllBrandOwner(@ModelAttribute BrandListRequestBean requestBean) {
        if (requestBean.getIsMap() != null && !ArrayUtils.contains(new Integer[]{0, 1}, requestBean.getIsMap())) {
            throw new BizException(Meta.CODE_451.getCode(), "isMap参数非法");
        }

        return ResultData.newInstance(brandOwnerService.getBranOwners(requestBean));
    }

    @ApiOperation(value = "PC_获取品牌关联方案统计数据", notes = "对应老接口：v1/pc/common/data/brands/cal",
            response = BrandOwnerDetailResponse.class, httpMethod = "GET")
    @GetMapping("/sol_count")
    public ResultData queryAllBrand(@ApiParam(value = "获取数量", required = true) @RequestParam("num") Integer num) {
        if (!NumberHelper.validatePositive(num)) {
            throw new BizException(Meta.CODE_451.getCode(), "num参数非法");
        }

        return ResultData.newInstance(baseService.getBrandCal(num));
    }

    @ApiOperation(value = "PC_根据品牌分类获取品牌的数据", notes = "对应老接口：v1/pc/common/data/brands/type",
            response = BrandOwnerDetailResponse.class, httpMethod = "GET")
    @GetMapping("/type/{typeId}")
    public ResultData queryBrandsByType(
            @ApiParam(value = "类型编号", required = true) @PathVariable("typeId") Integer typeId) {
        if (!NumberHelper.validatePositive(typeId)) {
            throw new BizException(Meta.CODE_451.getCode(), "typeId参数非法");
        }

        return ResultData.newInstance(brandOwnerService.getBrandsByType(typeId));
    }

    @ApiOperation(value = "PC_品牌厂商详情（根据品牌查找品牌主）", response = BrandFirmDetailResponseBean.class, httpMethod = "GET")
    @GetMapping("/user/{brandId}")
    public ResultData getBrandFirmDetail(
            @ApiParam(value = "品牌ID", required = true) @PathVariable("brandId") Integer brandId,
            @ApiParam(value = "当前登陆用户ID") Integer loginUserId) {
        if (!NumberHelper.validatePositive(brandId)) {
            return ResultData.newInstance(new Meta(Meta.CODE_451.getCode(), "品牌编号不合法!"));
        }

        return ResultData.newInstance(brandFirmsService.getBrandFirmDetail(brandId, loginUserId));
    }
}
